package net.hn.hnms.biz.emgcy.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import lombok.Data;
import lombok.EqualsAndHashCode;
import net.hn.hnms.biz.emgcy.domain.MaterialPo;
import net.hn.hnms.biz.emgcy.service.MaterialSrvice;
import net.hn.hnms.biz.emgcy.template.MaterialTemplate;
import net.hn.hnms.sys.system.api.RemoteDictService;
import net.hn.hnms.sys.system.api.domain.SysDictData;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
 * @param * @param null
 * @author baijiajun
 * @date 2022/1/20
 * @return
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class MaterialImportListener extends AnalysisEventListener<MaterialTemplate> {

    private MaterialSrvice materialSrvice;

    private RemoteDictService remoteDictService;

    public MaterialImportListener(MaterialSrvice materialSrvice, RemoteDictService remoteDictService) {
        this.materialSrvice = materialSrvice;
        this.remoteDictService = remoteDictService;
    }

    private List<MaterialTemplate> list = new ArrayList<>();

    @Override
    public void invoke(MaterialTemplate materialTemplate, AnalysisContext context) {
        this.list.add(materialTemplate);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        ArrayList<MaterialPo> materials = new ArrayList<>();
        list.stream().forEach(new Consumer<MaterialTemplate>() {
            @Override
            public void accept(MaterialTemplate materialTemplate) {
                try {
                    MaterialPo material = new MaterialPo();
                    material.setName(materialTemplate.getName());
                    String[] split = materialTemplate.getType().split("/");
                    String str="";
                    for(String s:split){
                        SysDictData dict = remoteDictService.queryInfo("material_type", s);
                        if(null!=dict){
                            str+=dict.getDictValue()+",";
                            String type = str.substring(0, str.length() - 1);
                            material.setType(type);
                        }else{
                            material.setType("");
                        }
                    }
                    material.setDept(materialTemplate.getDept());
                    material.setPhone(materialTemplate.getPhone());
                    material.setQuantity(materialTemplate.getQuantity());
                    material.setUnit(materialTemplate.getUnit());
                    material.setSpecification(materialTemplate.getSpecification());
                    /*material.setTobuyAt(materialTemplate.getTobuyAt());
                    material.setPerformance(materialTemplate.getPerformance());
                    material.setProductAt(materialTemplate.getProductAt());
                    material.setValidityAt(materialTemplate.getValidityAt());*/
                    material.setAddress(materialTemplate.getAddress());
                    material.setDescription(materialTemplate.getDescription());
                    material.setKeeper(materialTemplate.getKeeper());
                    //material.setTel(materialTemplate.getTel());
                    materials.add(material);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        materialSrvice.saveBatch(materials);
        this.list.clear();
    }
}
